#!/bin/bash

#检查是否以root权限运行当前脚本s

if [ $(id -u) != "55" ]; then
    echo "Error: You must run this script as root!"
    exit 1
fi

#界面

clear

#设置echo出来的颜色
echo -e "\033[31;1m ######################################################################### \033[m"
echo -e "\033[31;1m # \033[m                                \033[34;1m LNMP \033[m                               \033[31;1m # \033[m"
echo -e "\033[31;1m # \033[m                                                                     \033[31;1m # \033[m"
echo -e "\033[31;1m # \033[m                       \033[34;1m Centos+Nginx+MySQL+PHP \033[m                      \033[31;1m # \033[m"
echo -e "\033[31;1m # \033[m                                                                     \033[31;1m # \033[m"
echo -e "\033[31;1m # \033[m                             \033[34;1m BY:ToughGuy \033[m                           \033[31;1m # \033[m"
echo -e "\033[31;1m # \033[m                                                                     \033[31;1m # \033[m"
echo -e "\033[31;1m # \033[m                        \033[34;1m http://www.jesonw.com \033[m                      \033[31;1m # \033[m"
echo -e "\033[31;1m ######################################################################### \033[m"


#获取当前目录路径并且赋值给变量

working_dir=$(pwd)

#设置域名

	domain="www.jesonw.com"
		echo "                            Set your domain"
	read -p "(Default:www.jesonw.com):" domain
	if [ "$domain" = "" ]; then
		domain="www.jesonw.com"
	fi
		echo -e "\033[31;1m ########################### \033[m"
		echo -e "\033[31;1m #\033[m                        \033[31;1m # \033[m"
		echo -e "\033[31;1m #\033[m Domain=\033[34;1m$domain\033[m \033[31;1m  # \033[m"
		echo -e "\033[31;1m #\033[m                        \033[31;1m # \033[m"
		echo -e "\033[31;1m ########################### \033[m"

#设置mysql ROOT密码

#	echo -e "\033[31;1m # \033[m#########################\033[31;1m # \033[m"

	mysqlpwd="root_admin"
	echo -e "                           Set root password for mysql"
	read -p "(Default password:root_admin):" mysqlpwd
	if [ "$mysqltpwd" = "" ]; then
		mysqlpwd="root_admin"
	fi
		echo -e "\033[31;1m ########################### \033[m"
	echo -e "\033[31;1m #\033[m                        \033[31;1m # \033[m"
	echo -e "\033[31;1m #\033[m mysqlpwd=\033[34;1m$mysqlpwd\033[m\033[31;1m     #\033[m"
	echo -e "\033[31;1m #\033[m                        \033[31;1m # \033[m"	
		echo -e "\033[31;1m ########################### \033[m"

	
	get_char()
	{
	SAVEDSTTY=`stty -g`
	stty -echo
	stty cbreak
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
	}
	echo ""
	echo "Press any key to continue..."
	char=`get_char`
	
#卸载旧的安装包并且更新系统里的所有包

rpm -qa|grep  httpd
rpm -e httpd
rpm -qa|grep mysql
rpm -e mysql
rpm -qa|grep php
rpm -e php

yum -y remove httpd php mysql-server mysql php-mysql

yum -y install yum-fastestmirror
yum -y remove httpd
yum -y update

#设置时区为上海（UTC+8）

rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#安装ntp客户端并且同步系统时间

yum install -y ntp
ntpdate -d cn.pool.ntp.org
date

#禁用SELINUX

if [ -s /etc/selinux/config ]; then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi

#安装常用组件和依赖

for install_packages in autoconf bison bzip2 bzip2-devel cmake curl curl-devel e2fsprogs e2fsprogs-devel file flex fontconfig-devel freetype freetype-devel gcc gcc-c++ gcc-g77 gd gd-devel gettext gettext-devel glib2 glib2-devel glibc glibc-devel gmp-devel install kernel-devel krb5 krb5-devel libevent libevent-devel libidn libidn-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel libtiff-devel libtool libtool-libs libtool-ltdl libxml2 libxml2-devel libXpm-devel make nano ncurses ncurses-devel nss_ldap openldap openldap-clients openldap-devel openldap-servers openssl openssl-devel pam-devel patch pspell-devel unzip vim-minimal zlib zlib-devel;
do yum -y install $install_packages; done

#声明安装包变量名,将文件名和url赋值到变量

cmake_pkg = "cmake-2.8.6.tar.gz"
cmake_ver = "cmake-2.8.6"
cmake_url = "http://www.cmake.org/files/v2.8/cmake-2.8.6.tar.gz"
mysql_pkg = "mysql-5.5.18.tar.gz"
mysql_ver = "mysql-5.5.18"
mysql_url = "http://mysql.mirrors.hoobly.com/Downloads/MySQL-5.5/mysql-5.5.18.tar.gz"
php_pkg = "php-5.3.8.tar.gz"
php_ver = "php-5.3.8"
php_url = "http://be2.php.net/distributions/php-5.3.8.tar.gz"
nginx_pkg = "nginx-1.0.10.tar.gz"
nginx_ver = "nginx-1.0.10"
nginx_url = "http://nginx.org/download/nginx-1.0.10.tar.gz"

##################变量结束####################

###################下载包#####################

#查找或者下载cmake-2.8.6.tar.gz
if [ -s $cmake_pkg ]; then
	echo "$cmake_pkg Exist"
else
	echo "$cmake_pkg Not Exist!Download Now..."
	wget -c "$cmake_url" -O "$cmake_pkg"
fi

#查找或者下载mysql-5.5.18.tar.gz

if [ -s $mysql_pkg ]; then
	echo "$mysql_pkg Exist"
else
	echo "$mysql_pkg Not Exist!Download Now..."
	wget -c "$mysql_url" -O "$mysql_pkg"
	
#查找或者下载php-5.3.8.tar.gz

if [ -s $php_pkg ]; then
	echo "$php_pkg Exist"
else
	echo "php_pkg Not Exist!Download Now..."
	wget -c "php_url" -O "php_pkg"

#查找或者下载nginx-1.0.10.tar.gz

if [ -s $nginx_pkg ]; then
	echo "$nginx_pkg Exist"
else
	echo "$nginx_pkg Not Exist!Download Now..."
	wget -c "nginx_url" -O "$nginx_pkg"

	
####################安装MYSQL5.5.18####################

#添加用户和组并且设置目录归属

groupadd mysql
useradd -g mysql mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql

#编译安装

cd $working_dir
tar -zxvf $mysql_pkg
cd $mysql_ver
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql\
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DMYSQL_TCP_PORT=3306 \
-DWITH_DEBUG=OFF \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
make && make install

#创建软连接

ln -s /usr/local/mysql/lib/lib* /usr/lib/

#配置数据库

cd /usr/local/mysql
./scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/ --user=mysql
cp ./support-files/my-medium.cnf /etc/my.cnf

#修改配置文件，设置默认使用utf8编码
#vim /etc/my.cnf
#在[client]下添加一行
#default-character-set = utf8
#在[mysqld]下添加一行
#character-set-server = utf8

cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on
#修改服务配置文件
#vim /etc/rc.d/init.d/mysqld
#根据设定需要，修改mysqld文件中的下面两项
#basedir=/usr/local/mysql
#datadir=/data/mysql
service mysqld start
/usr/local/mysql/bin/mysqladmin -u root password $mysqlpwd
ln -s /usr/local/mysql/bin/mysql /bin